package com.rui.study.algorithm.p_比较两个集合元素是否相同__指纹比较;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @program: study_20180903
 * @description:
 * @author: Yaowr
 * @create: 2018-11-05 08:53
 **/
public class Solution {

    private static final char[] CHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    public boolean solution(String[] set1, String[] set2) {
        String c1 = calcualte(set1);
        String c2 = calcualte(set2);
        return c1.equals(c2);
    }

    private String calcualte(String[] set1) {
        BigInteger count = BigInteger.valueOf(0l);
        for (String element : set1) {
            count = count.add(new BigInteger(signature(element), 16));
        }
        return count.toString();
    }

    public String signature(String s) {
        StringBuffer sb = new StringBuffer();
        try {
            MessageDigest digest = MessageDigest.getInstance("md5");
            digest.update(s.getBytes());
            byte[] bytes = digest.digest();
            for (byte b : bytes) {
                int h = (b & 0xf0) >> 4;
                sb.append(CHARS[h]);
                int l = b & 0x0f;
                sb.append(CHARS[l]);
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String[] set1 = {"", "jdka", "7", "ba", "cc1"};
        String[] set2 = {"cc1", "ba", "jdka", "7", ""};
        System.out.println(new Solution().solution(set1, set2));
    }
}
